$(document).ready(function() {
	moment.locale("zh-cn");
	window.currentpage = 1;
	ready();
	
});

function ready() {
	queryUserInfo(1);//查询第一页
	//绑定事件
	bind();
	check();
	showUserName();
}

function bind() {
	$("#pageSize").bind("change",function(){
		queryUserInfo(window.currentpage);
	});
	$("#searchStuByStuid").bind("click",function(){
		window.currentpage = 1;
		queryUserInfo(window.currentpage);
	});
	$('#getDormAdminInfoModal').on('hide.bs.modal', function (){
		$("#getDormAdminId").val("");
		$("#getDormAdminName").val("");
		$("#getDormAdminBirthday").val("");
		$("#getDormAdminSex").val("");
		$("#getDormAdminTel").val("");
		$("#getDormAdminId-error").remove();
		$("#getDormAdminName-error").remove();
		$("#getDormAdminBirthday-error").remove();
		$("#getDormAdminSex-error").remove();
		$("#getDormAdminTel-error").remove();
	});
	$('#updatePasswordModel').on('hide.bs.modal', function (){
		$("#oldPassword").val("");
		$("#password").val("");
		$("#password_again").val("");
		$("#oldPassword-error").remove();
		$("#password-error").remove();
		$("#password_again-error").remove();
	});
}

function check(){
	updateInfoCheck();
	updatePasswordCheck();
}

function updatePasswordCheck(){
	$("#updatePassword").validate({
		rules:{
			oldPassword:{
				required:true
			},
			password:{
				required:true
			},
			password_again:{
				required:true,
				equalTo: "#password"
			}
		},
		messages:{
			oldPassword:{
				required:"请输入旧密码"
			},
			password:{
				required:"请输入新密码"
			},
			password_again:{
				required:"请输入新密码",
				equalTo: "两次输入不相同"
			}
		},
		submitHandler:function(form){
			var oldPassword = $("#oldPassword").val();
			var password = $("#password").val();
			$.ajax({
				type:'post',
				url:'updatePassword',
				data:{"oldPassword":oldPassword,"password":password},
				success:function(data){
				},
				error:function(){
					console.log("1");
				}
			});
			$('#updatePasswordModel').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function updateInfoCheck(){
	$("#getDormAdminInfo").validate({
		rules:{
			getDormAdminName:{
				required:true
			},
			getDormAdminName:{
				required:true
			}
		},
		messages:{
			getDormAdminName:{
				required:"请输入姓名"
			},
			getDormAdminSex:{
				required:"请选择性别"
			}
		},
		submitHandler:function(form){
			var dormAdminId = $("#getDormAdminId").val();
			var dormAdminName = $("#getDormAdminName").val();
			var dormAdminSex = $("#getDormAdminSex").val();
			var dormAdminBirthday = new Date($("#getDormAdminBirthday").val());
			var dormAdminTel = $("#getDormAdminTel").val();
			$.ajax({
				type:'post',
				url:'updateDormAdminInfo',
				data:{"dormAdminId":dormAdminId,"dormAdminName":dormAdminName,"dormAdminSex":dormAdminSex,"dormAdminBirthday":dormAdminBirthday,"dormAdminTel":dormAdminTel},
				success:function(data){
				},
				error:function(){
					console.log("1");
				}
			});
			$('#getDormAdminInfoModal').modal('hide');
		},
		invalidHandler: function(form, validator) {return false;}
	})
}

function queryUserInfo(page){
	var currPage = page;
	var pageSize = $("#pageSize").val();
	var stuStatus = $("#stuStatus").val();
	var stuId = $("#stuId").val();
	$.ajax({
		url:"/dormms/admin/queryStuInfoByPage",
        type:"post",
        data:{"currPage":currPage,"pageSize":pageSize,"stuId":stuId,"isLeave":stuStatus},
        success:function(data){
        	console.log(data);
        	$("#stuInfoList").empty();
        	$("#total").text("共有"+data.total+"条记录");
        	for(var i=0; i<data.list.length; i++){
        		 var trTemp = $("<tr></tr>");
        		 //trTemp.append("<td>"+ "<input type='checkbox' name='selectStu' value='" + data.list[i].stuId + "'></td>");
        		 trTemp.append("<td id='findStuId'>"+ data.list[i].stuId +"</td>");
        		 trTemp.append("<td>"+ data.list[i].stuName +"</td>");
        		 trTemp.append("<td>"+ data.list[i].stuGrade +"</td>");
        		 if(data.list[i].stuTel != null){
        			 trTemp.append("<td>"+ data.list[i].stuTel +"</td>");
        		 }
        		 else{
        			 trTemp.append("<td></td>");
        		 }
        		 trTemp.append("<td><button type='button' class='btn btn-outline-success btn-sm' onclick='getStuInfoGet(this)'>查看</button></td>");
        		 trTemp.appendTo("#stuInfoList");
        	}
        	queryUserRoleByPage(data);
        }
	});
}

function queryUserRoleByPage(data) {
	var options = {
			bootstrapMajorVersion: 3,
			currentPage: data.pageNum,
            totalPages: data.pages,
            numberofPages: 5,
            itemTexts: function(type, page, current) { //修改显示文字
                switch (type) {
                case "first":
                    return "&laquo;";
                case "prev":
                    return "&lt;";
                case "next":
                    return "&gt;";
                case "last":
                    return "&raquo;";
                case "page":
                    return page;
                }
            },
            onPageClicked: function (event, originalEvent, type, page) { //异步换页
            	queryUserInfo(page);
            	window.currentpage=page;
            },
	};
	$('#page').bootstrapPaginator(options); 
}


//获取当前行数据(查看)
function getStuInfoGet(obj) {
	var x = $(obj).parent().parent().find("td");
	var stuinfo = x.eq(0).text();
	$.ajax({
		url:"/dormms/admin/queryStuInfoByStuId",
		type:"get",
		data:{'stuId':stuinfo},
		success:function(data){
			console.log(data);
			$("#getStuId").val(data.stuId);
			$("#getStuName").val(data.stuName);
			$("#getStuSex").val(data.stuSex);
			$("#getStuGrade").val(data.stuGrade);
			console.log(data.stuBirthday);
			if(data.stuBirthday!=null){
				$("#getStuBirthday").val(moment(data.stuBirthday).format('L'));
			}else{
				$("#getStuBirthday").val("");
			}
			$("#getStuTel").val(data.stuTel);
			$("#getStuParentTel").val(data.stuParentTel);
			$('#getStuInfoModal').modal('show');
		}
	});
}

function showDormAdminInfoModal() {
	$.ajax({
		url:"getDormAdminInfo",
		type:"get",
		success:function(data){
			console.log(data);
			$("#getDormAdminId").val(data.dormAdminId);
			$("#getDormAdminName").val(data.dormAdminName);
			$("#getDormAdminSex").val(data.dormAdminSex);
			if(data.dormAdminBirthday!=null){
				$("#getDormAdminBirthday").val(moment(data.dormAdminBirthday).format('L'));
			}else{
				$("#getDormAdminBirthday").val("");
			}
			$("#getDormAdminTel").val(data.dormAdminTel);
			$("#getDormAdminParentTel").val(data.dormAdminParentTel);
			$('#getDormAdminInfoModal').modal('show');
		}
	});
}

function showPasswordModel() {
	$('#updatePasswordModel').modal('show');
}

function showUserName() {
	$.ajax({
		url:"getDormAdminInfo",
		type:"get",
		success:function(data){
			$("#userName").text(data.dormAdminName);
		}
	});
}
